﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHTU;
using NOMFinalSettlement.DatasetReport;
using NOMFinalSettlement.CrystalReport;
namespace NOMFinalSettlement.AppForm.Program
{
    public partial class fReportFinishDetails : DevComponents.DotNetBar.Office2007Form
    {
        FinalSettlementModel.TableHoSo profile = null;

        string formatMoney = "{0:0,0.00}";

        public fReportFinishDetails()
        {
            InitializeComponent();
        }

        private void btnProcessing_Click(object sender, EventArgs e)
        {
            if (bgw.IsBusy != true)
            {
                bgw.RunWorkerAsync();
            }
        }

        private void timerLoading_Tick(object sender, EventArgs e)
        {
            circularProgress.Value += 2;
        }

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            EnableCircular(true);
            btnProcessing.Enabled = false;
            lbStatus.Text = " Đang xử lý ...";
            doSearch();
        }

        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            EnableCircular(false);
            btnProcessing.Enabled = true;
            lbStatus.Text = "";
        }
        void EnableCircular(bool enable)
        {
            circularProgress.Visible = enable;
            circularProgress.IsRunning = enable;
            if (enable)
            {
                timerLoading.Start();
            }
            else
            {
                timerLoading.Stop();
            }
        }

        //Xử lý
        void doSearch()
        {
            dsReportFinishDetail reportFinishDetailds = new dsReportFinishDetail();
            var tblProfile = profile.listProfile().Where(w => w.HoanThanhQuyetToan == "Đã hoàn thành");

            var _dateStart = IsText(dateStart);
            var _dateEnd = IsText(dateEnd);
            if (dateEnd.Value < dateStart.Value)
            {
                MsgBox.Show("Điều kiện lọc không đúng. Vui lòng nhập lại!", "Thông Báo", MsgBoxButton.YesNo, MsgBoxIcon.Stop);
                return;
            }
            if (dateStart.Text != "")
            {
                tblProfile = tblProfile.Where(w => w.NgayNhan >= dateStart.Value);
            }
            if (dateEnd.Text != "")
            {
                tblProfile = tblProfile.Where(w => w.NgayNhan <= dateEnd.Value);
            }
            int stt = 1;
            foreach (var rowProfile in tblProfile)
            {
                DataRow row = reportFinishDetailds.dtReportFinishDetail.NewRow();
                row["colSTT"] = stt;
                row["colSoLuu"] = rowProfile.SoLuu;
                row["colTenDuAn"] = rowProfile.TenDuAn;
                row["colChuDauTu"] = rowProfile.ChuDauTu;
                row["colGiaTriDeNghiQT"] = String.Format(formatMoney,rowProfile.DeNghiGiaTriQuyetToan);
                row["colGiaTriQuyetToan"] = String.Format(formatMoney, rowProfile.GiaTriQuyetToan);
                row["colNgayHoanThanh"] = rowProfile.NgayPheDuyet.Value.ToShortDateString();
                row["colCanBoThamTra"] = rowProfile.CanBo;
                reportFinishDetailds.dtReportFinishDetail.Rows.Add(row);
                stt++;
            }
            cReportFinishDetails reportFinishDetailcr = new cReportFinishDetails();
            reportFinishDetailcr.SetDataSource(reportFinishDetailds);
            Invoke(new MethodInvoker(() => crystalReportViewer1.ReportSource = reportFinishDetailcr), null);
            reportFinishDetailcr.SetParameterValue("dateFrom", dateStart.Text);
            reportFinishDetailcr.SetParameterValue("dateTo", dateEnd.Text);
        }

        string IsText(Control c)
        {
            var _self = MainApp.Self.appProcess;
            return _self.crossText(c);
        }

        private void fReportFinishDetails_Load(object sender, EventArgs e)
        {
            profile = new FinalSettlementModel.TableHoSo();
        }
    }
}
